import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from lxml import html
import time
from datetime import datetime
def get_weather_data(city, year, month):
    # 配置Edge选项
    edge_options = Options()
    edge_options.add_argument("--headless")  # 无头模式，不打开浏览器窗口
    edge_options.add_argument("--disable-gpu")

    # 指定EdgeDriver路径
    service = Service('D:\\edgediver\\msedgedriver.exe')

    # 初始化WebDriver
    driver = webdriver.Edge(service=service, options=edge_options)

    if month < 10:
        month = '0' + str(month)
    else:
        month = str(month)
    # 打开目标网页
    url = f'https://lishi.tianqi.com/{city}/{year}{month}.html'
    driver.get(url)

    # 等待页面加载
    time.sleep(3)

    # 找到“查看更多”按钮并点击
    try:
        more_button = driver.find_element(By.CLASS_NAME, 'lishidesc2')
        more_button.click()
    except Exception as e:
        print(f"该网页尚未更新最新的每月总结，您本月尚未结束，请结束后再来收集！！")

    # 等待更多内容加载
    time.sleep(3)

    # 获取页面源码
    html_content = driver.page_source

    # 解析HTML
    tree = html.fromstring(html_content)

    # 提取所有天气预报条目
    forecast_items = tree.xpath('//ul[@class="thrui"]/li')

    weather_info = []
    # 遍历每个条目并提取信息
    for item in forecast_items:
        day_weather_info = {}
        day_weather_info['date'] = item.xpath('.//div[@class="th200"]/text()')[0].strip()
        day_weather_info['high_temp'] = item.xpath('.//div[@class="th140"][1]/text()')[0].strip()
        day_weather_info['low_temp'] = item.xpath('.//div[@class="th140"][2]/text()')[0].strip()
        day_weather_info['weather'] = item.xpath('.//div[@class="th140"][3]/text()')[0].strip()
        day_weather_info['wind'] = item.xpath('.//div[@class="th140"][4]/text()')[0].strip()
        weather_info.append(day_weather_info)


    # 关闭浏览器
    driver.quit()
    return weather_info

def save_weather():
    year_weather_data = []
    # 获取当前时间防止爬取没有月份的网页
    current_time = datetime.now()
    current_month = current_time.month
    current_year = current_time.year
    # current_day = current_time.day
    #输入您的详细城市名称
    city = 'dalian'
    for i in range(1, current_month+1):
        year_weather_data.extend(get_weather_data(city, current_year, i))


    # 定义CSV文件路径
    csv_file_path = f'D:\\python_project\\Dazuoye\\{city}_weather_{current_year}.csv'

    # 写入CSV文件
    with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        writer.writerow(['日期', '最高气温', '最低气温', '天气', '风级'])
        writer.writerows([data['date'], data['high_temp'], data['low_temp'], data['weather'], data['wind']] for data in year_weather_data)

save_weather()


